from django.template import Library
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse

register = Library()

@register.filter
def tag_add_url(object, tag_id):
    content_type = ContentType.objects.get_for_model(object)
    return reverse("tags-add", kwargs=dict(content_type_id=content_type.id, object_id=object.id, tag_id=tag_id))

@register.filter
def tag_add_url_template(object, template):
    content_type = ContentType.objects.get_for_model(object)
    s = reverse("tags-add", kwargs=dict(content_type_id=content_type.id, object_id=object.id, tag_id=9999999))
    return s.replace('9999999', template)


@register.filter
def tag_remove_url_template(object, template):
    content_type = ContentType.objects.get_for_model(object)
    s = reverse("tags-remove", kwargs=dict(content_type_id=content_type.id, object_id=object.id, tag_id=9999999))
    return s.replace('9999999', template)


@register.simple_tag
def active(request, view, **kwargs):
    from django.core.urlresolvers import NoReverseMatch
    try:
      if request.path == reverse(view, kwargs=kwargs):
        return 'active'
    except NoReverseMatch:
      pass
    except AttributeError as e:
      print e
    return ''
